﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.Text;
using FeedAgregator;

public partial class ContactUs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void OKButton_Click(object sender, EventArgs e)
    {

        NoBotState state;

        // if condition to check the response state of NoBot
        if (NoBot1.IsValid(out state))
        {
            //AddFeedBackToDatabase
            using (FeedSignalDataContext ctx = new FeedSignalDataContext())
            {
                VisitorMessage messageToAdd = new VisitorMessage();
                messageToAdd.Message = MessageTextBox.Text;
                messageToAdd.VisitorEmail = EmailTextBox.Text;
                messageToAdd.VisitorName = NameTextBox.Text;
                messageToAdd.MessageDate = DateTime.Now;
                ctx.VisitorMessages.InsertOnSubmit(messageToAdd);
                ctx.SubmitChanges();
            }
        }
        else
        {

        }

        StringBuilder sb = new StringBuilder();
        // foreach loop to get cached IP address and datetime assocated with it(when last postback was occurred)
        foreach (KeyValuePair<DateTime, string> keyValue in NoBot.GetCopyOfUserAddressCache())
        {
            sb.AppendFormat("{0}: {1}<br />", keyValue.Key.ToString(), keyValue.Value);
        }
        Response.Write(sb.ToString());
    }

    protected void NoBot1_GenerateChallengeAndResponse(object sender, NoBotEventArgs e)
    {
        // This is a sample challenge/response implementation that involves
        // the DOM so as to make the calculation more difficult to thwart.
        // It adds a randomly sized Panel; the client must calculate the area.
        Panel p = new Panel();
        p.ID = "NoBotSamplePanel";
        Random rand = new Random();
        p.Width = rand.Next(300);
        p.Height = rand.Next(200);
        p.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
        p.Style.Add(HtmlTextWriterStyle.Position, "absolute");
        ((NoBot)sender).Controls.Add(p);
        e.ChallengeScript = string.Format("var e = document.getElementById('{0}'); e.offsetWidth * e.offsetHeight;", p.ClientID);
        e.RequiredResponse = (p.Width.Value * p.Height.Value).ToString();
    }

}
